//
// Created by meitu on 2024/10/9.
//

#ifndef COLORBYTEDESKTOP_MRESTRICTEDLINEEDIT_H
#define COLORBYTEDESKTOP_MRESTRICTEDLINEEDIT_H

#include <QDoubleValidator>
#include <QIntValidator>
#include <QLineEdit>

class CustomIntValidator : public QIntValidator
{
    Q_OBJECT
  public:
    explicit CustomIntValidator(QObject *parent = nullptr);

    CustomIntValidator(int bottom, int top, QObject *parent);
    void setRange(int bottom, int top) override;
    virtual State validate(QString &s, int &n) const override;
    virtual void fixup(QString &input) const override;
};

class CustomDoubleValidator : public QDoubleValidator
{
    Q_OBJECT
  public:
    explicit CustomDoubleValidator(QObject *parent = nullptr);

    CustomDoubleValidator(double bottom, double top, int decimals = 0, QObject *parent = nullptr);
    void setRange(double bottom, double top, int decimals = 0) override;
    virtual State validate(QString &s, int &n) const override;
    virtual void fixup(QString &input) const override;
};

class MRestrictedLineEdit : public QLineEdit
{
    Q_OBJECT

  public:
    explicit MRestrictedLineEdit(QWidget *parent = nullptr);
    ~MRestrictedLineEdit() override;

  public:
    void setRange(int minimum, int maximum);
    void setValue(int value);
    void setSingleStep(int step);
    void setEnableDifferentValue(bool enable);
    void setIntValidator(int minimum, int maximum);
    void setDoubleValidator(double minimum, double maximum, int decimals = 2);

  signals:
    void valueChanging(int value);
    void valueChanged(int value);

  protected:
    void contextMenuEvent(QContextMenuEvent *event) override;
    void focusInEvent(QFocusEvent *event) override;
    void keyPressEvent(QKeyEvent *ev) override;
    void keyReleaseEvent(QKeyEvent *event) override;
    void wheelEvent(QWheelEvent *e) override;

  private:
    inline void updateText(int value);

  private:
    CustomIntValidator *customIntValidator_{};
    CustomDoubleValidator* customDoubleValidator_{};
    int32_t minValue_{0};
    int32_t maxValue_{0};
    int32_t curValue_{0};
    int32_t singleStep_{1};
    bool differentValueEnable_{false};
};

#endif // COLORBYTEDESKTOP_MRESTRICTEDLINEEDIT_H
